package weiyao.Vul;

import us.codecraft.webmagic.Page;
import weiyao.Detector.BaseDetector;
import weiyao.GUI.ScanResultCallback;
import weiyao.SiteManager;

import java.util.EnumMap;
import java.util.Map;
import java.util.Set;

public class VulnerabilityScanner {
    private final Map<VulnerabilityConfig.VulnerabilityType, VulnerabilityDetector> detectors;
    private final SiteManager siteManager;
//漏洞扫描配置
    public VulnerabilityScanner(Set<VulnerabilityConfig.VulnerabilityType> enabledScans, SiteManager siteManager) {
        this.siteManager = siteManager;
        this.detectors = new EnumMap<>(VulnerabilityConfig.VulnerabilityType.class);

        for (VulnerabilityConfig.VulnerabilityType type : enabledScans) {
            try {
                Class<? extends VulnerabilityDetector> detectorClass =
                        VulnerabilityConfig.getDetectorClass(type);
                if (detectorClass != null) {
                    VulnerabilityDetector detector = detectorClass.getDeclaredConstructor().newInstance();
                    if (detector instanceof BaseDetector) {
                        ((BaseDetector) detector).setSiteManager(siteManager);
                    }
                    detectors.put(type, detector);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void scanPage(Page page, ScanResultCallback callback) {
        String baseUrl = page.getUrl().get();
        for (VulnerabilityDetector detector : detectors.values()) {
            detector.detect(page, baseUrl, callback);
        }
    }
}